/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBaseClass;
	import com.googlecode.milib.data.MLCollection;
	import com.googlecode.milib.tools.MLEventsReflectorFromCollection;
	import com.googlecode.milib.util.MLMCUtil;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGStage extends MLBaseClass implements IMLBroadcasterListener  {

		private static var instance:MLDBGStage;		
		private var mc:MovieClip;
		private var ele0:MovieClip;
		private var windowsEvents:MLEventsReflectorFromCollection;
		private var selectedWindow:MLDBGWindow;
		private var windows:MLCollection;
		
		public function MLDBGStage(access:Private, mc:MovieClip) {
			this.mc=mc;
			ele0=mc.ele0;
			mc.removeChild(ele0);
			
			windows=new MLCollection();
			windows.addListener(this);
			
			windowsEvents=new MLEventsReflectorFromCollection(false, windows);
			windowsEvents.addListener(this);
		}
		
		public function getNewMLDBGMC():MovieClip {
			return MovieClip(MLMCUtil.duplicateDisplayObject(ele0, false));
		}		
		
		public function addMLDBGWindow(window:MLDBGWindow):void {
			windows.add(window);
		}
		
		public static function getInstance():MLDBGStage {
			if(instance) { return instance; }
			else{ throw new Error('Before call MLDBGStage.getInstance() you should call MLDBGStage.init()'); }
		}
		
		public function setAllWindowsPositionsTo0():void {
			var windowsArr:Array=windows.toArray();
			var i:uint;
			var loop:MLDBGWindow;
			for(i=0;i<windowsArr.length;i++){
				loop=windowsArr[i];
				loop.setPosition(0, 0);
			}
		}
		
		public static function init(mc:MovieClip):void {
			if(instance) { return; }
			else{ instance=new MLDBGStage(null, mc);}
		}
		
//********************************************************************************************
//		events for MLDBGStage
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case windowsEvents:
					if(ev.event!=windowsEvents.event_EventReflection){ break; }
					ev=ev.data;
					var hero:MLDBGWindow=MLDBGWindow(ev.hero);
					switch(ev.event) {
						case hero.event_Select:
							if(selectedWindow!=hero){
								if(selectedWindow){
									MLMCUtil.setMCDepthToFront(hero.mlmc.mc);
									selectedWindow.setIsSelected(false);
								}
								selectedWindow=hero;
							}
						break;
						case hero.event_Close:
							windows.remove(hero);
						break;
					}
				break;
				case windows:
					switch(ev.event) {
						case windows.event_ValueAdded:
							mc.addChild(MLDBGWindow(ev.data).mlmc.mc);
						break;
						case windows.event_ValueRemoved:
							mc.removeChild(MLDBGWindow(ev.data).mlmc.mc);
						break;
					}
				break;
			}
		}
		
	}
}

internal class Private {}
